package com.chat.controller;


import com.chat.response.BaseResponse;
import com.chat.response.FriendResponse;
import com.chat.response.GroupSetResponse;
import com.chat.service.ChatUserFriendService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/cat/message")
public class MessageController {
    @Resource
    ChatUserFriendService catUserFriendService;

    @GetMapping("/getFriendList/{id}")
    public BaseResponse<ArrayList<GroupSetResponse>> getFriendList(@PathVariable String id) {
        List<FriendResponse> friendList = catUserFriendService.getFriendList(Long.valueOf(id));
        ArrayList<GroupSetResponse> groupSetVos = new ArrayList<>();
        Map<String, List<FriendResponse>> collect = friendList.stream().collect(Collectors.groupingBy(FriendResponse::getGroupName));
        collect.forEach((key,value) -> {
            GroupSetResponse groupSetVo = new GroupSetResponse();
            groupSetVo.setGroupName(key);
            groupSetVo.setList(value);
            groupSetVos.add(groupSetVo);
        });
        return BaseResponse.success(groupSetVos);
    }
}
